//
//  ModelRepayment.m
//  MexicoLoan
//
//  Created by WXP on 2025/3/4.
//
//

#import "ModelRepayment.h"

NSString *const kModelRepaymentRepaymentAmount = KEY_repaymentAmount;
NSString *const kModelRepaymentRepaymentTime = KEY_repaymentTime;

NSString *const kModelRepaymentRepayTime = KEY_repayTime;
NSString *const kModelRepaymentRepaymentIncomeAmount = KEY_repaymentIncomeAmount;

@interface ModelRepayment ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation ModelRepayment

@synthesize repaymentAmount = _repaymentAmount;
@synthesize repaymentTime = _repaymentTime;
@synthesize repayTime = _repayTime;
@synthesize repaymentIncomeAmount = _repaymentIncomeAmount;

+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict {
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict {
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    if (self && [dict isKindOfClass:[NSDictionary class]]) {
        self.repaymentAmount = [[self objectOrNilForKey:kModelRepaymentRepaymentAmount fromDictionary:dict] doubleValue];
        self.repaymentTime = [[self objectOrNilForKey:kModelRepaymentRepaymentTime fromDictionary:dict] doubleValue];

        self.repayTime = [[self objectOrNilForKey:kModelRepaymentRepayTime fromDictionary:dict] doubleValue];
        self.repaymentIncomeAmount = [[self objectOrNilForKey:kModelRepaymentRepaymentIncomeAmount fromDictionary:dict] doubleValue];
    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation {
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:[NSNumber numberWithDouble:self.repaymentAmount] forKey:kModelRepaymentRepaymentAmount];
    [mutableDict setValue:[NSNumber numberWithDouble:self.repaymentTime] forKey:kModelRepaymentRepaymentTime];
    
    [mutableDict setValue:[NSNumber numberWithDouble:self.repayTime] forKey:kModelRepaymentRepayTime];
    [mutableDict setValue:[NSNumber numberWithDouble:self.repaymentIncomeAmount] forKey:kModelRepaymentRepaymentIncomeAmount];

    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description  {
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict {
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}

@end
